import { loginAPI,phoneLogin, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken,setUserInfo,removeUserInfo,getUserInfo } from '@/utils/auth'
import { resetRouter } from '@/router'
import {  Message } from 'element-ui'

const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo:getUserInfo(),
    tagList:[{name:'数智中心',url:'/workBench',icon:'el-icon-s-data',title:'workBench',component:'workBench'}],
    defaultUrl:'workBench',
    lotsAry:[]
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_user_info:(state, data) => {
    state.userInfo = data;
  },
  ADD_TAGS:(state, data) => {
    state.tagList.push(data);
  },
  DEL_TAG:(state, i)=>{
    state.tagList.splice(i,1);
  },
  SAVE_DEFAULTURL:(state, path)=>{
    state.defaultUrl=path;
  },
  SET_LOTS:(state, item) => {
    state.lotsAry.push(item);
  },
  CLEARL_LOTS:(state, item) => {
    state.lotsAry=item;
  },
}

const actions = {
  setLotsData({ commit },item){
    commit('SET_LOTS',item);
  },

  // user login
  loginAPI({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      loginAPI({ username: username.trim(), password: password }).then(response => {
        const { data } = response;
        commit('SET_TOKEN', data.token)
        setToken(data.token);
        resolve()
      }).catch(error => {
        if(error.code == 40001){
          Message({
            message: error.msg ,
            type: 'warning'
         })
        }
      })
    })
  },
  phoneLogin({ commit }, userInfo) {
    const { mobile, code } = userInfo
    return new Promise((resolve, reject) => {
      phoneLogin({ mobile: mobile.trim(), code: code }).then(response => {
        const { data } = response;
        commit('SET_TOKEN', data.token)
        setToken(data.token);
        resolve()
      }).catch(error => {
        if(error.code == 40001){
          Message({
            message: error.msg ,
            type: 'warning'
         })
        }
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response

        if (!data) {
          return reject('验证失败，请重新登录')
        }
        commit('SET_user_info', data);
        setUserInfo(data);
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 多标签页存储
  ADD_TAGS({commit},data){
    commit('ADD_TAGS',data)
  },
  DEL_TAG({commit},i){
    commit('DEL_TAG',i)
  },
  SAVE_DEFAULTURL({commit},path){
    commit('SAVE_DEFAULTURL',path);
  },
   
  
  // user logout
  logout({ commit },id) {
    return new Promise((resolve, reject) => {
      logout({id:id}).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE');
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

